package hcmus.fit.master.rdfa

import grails.converters.JSON;

import org.springframework.dao.DataIntegrityViolationException

class TripleController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def upload = {
        def data = JSON.parse(params.data)
        println data
        for (def tripleData : data) {
            def triple = Triple.findBySubjectAndPredicate(tripleData.subject, tripleData.predicate)
            if(triple == null) {
                triple = new Triple(tripleData)
            } else {
                triple.object = tripleData.object
            }
            triple.save()
        }
        render(success: 'true')
    }

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [tripleInstanceList: Triple.list(params), tripleInstanceTotal: Triple.count()]
    }

    def create() {
        [tripleInstance: new Triple(params)]
    }

    def save() {
        def tripleInstance = new Triple(params)
        if (!tripleInstance.save(flush: true)) {
            render(view: "create", model: [tripleInstance: tripleInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'triple.label', default: 'Triple'), tripleInstance.id])
        redirect(action: "show", id: tripleInstance.id)
    }

    def show() {
        def tripleInstance = Triple.get(params.id)
        if (!tripleInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'triple.label', default: 'Triple'), params.id])
            redirect(action: "list")
            return
        }

        [tripleInstance: tripleInstance]
    }

    def edit() {
        def tripleInstance = Triple.get(params.id)
        if (!tripleInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'triple.label', default: 'Triple'), params.id])
            redirect(action: "list")
            return
        }

        [tripleInstance: tripleInstance]
    }

    def update() {
        def tripleInstance = Triple.get(params.id)
        if (!tripleInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'triple.label', default: 'Triple'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (tripleInstance.version > version) {
                tripleInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'triple.label', default: 'Triple')] as Object[],
                          "Another user has updated this Triple while you were editing")
                render(view: "edit", model: [tripleInstance: tripleInstance])
                return
            }
        }

        tripleInstance.properties = params

        if (!tripleInstance.save(flush: true)) {
            render(view: "edit", model: [tripleInstance: tripleInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'triple.label', default: 'Triple'), tripleInstance.id])
        redirect(action: "show", id: tripleInstance.id)
    }

    def delete() {
        def tripleInstance = Triple.get(params.id)
        if (!tripleInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'triple.label', default: 'Triple'), params.id])
            redirect(action: "list")
            return
        }

        try {
            tripleInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'triple.label', default: 'Triple'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'triple.label', default: 'Triple'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
